const { myOrderList, getReportTypeList } = require('../../utils/api');
const { formatDate } = require('../../utils/tool');
const STATUSMAP = { 0: null, 1: 0, 2: 1, 3: 2, 4: 3, 5: 4 };
Page({
  data: {
    active: null,
    orderList: [],
    typeMap: {},
  },

  /**
   * @param {*} options
   * @param {*} options.active
   */
  onLoad(options) {
    this.setData({
      active: Number(options?.active || 1),
    });
    this.getReportTypeList();
  },

  getReportTypeList() {
    getReportTypeList().then((res) => {
      const typeList = (res?.data || []).map((item) => {
        return {
          dictLabel: item.dictLabel,
          dictType: item.dictType,
        };
      });
      const typeMap = typeList.reduce((acc, cur) => {
        acc[cur.dictType] = cur.dictLabel;
        return acc;
      }, {});
      this.setData({ typeMap });
    });
  },

  getOrderList() {
    const params = {};
    if (STATUSMAP[this.data.active] !== null) {
      params.status = STATUSMAP[this.data.active];
    }
    myOrderList(params).then((res) => {
      this.setData({
        orderList: (res?.data || []).map((item) => {
          return {
            ...item,
            imgs: item.imgs?.split?.(',') || [],
            createTime: formatDate(item.createTime),
          };
        }),
      });
    });
  },

  onChange(e) {
    const { index } = e.detail;
    this.setData({ active: index });
    this.getOrderList();
  },
});
